<?
	require_once '_uniquedir.php';

	class Fmgr0v0r01_Drafts extends Fmgr0v0r01__uniquedir{
	
		protected $rdk = 'temp';
		protected $mod = 775;
		
		protected $valid_child_classnames = 'draft,htaccess';
		
		protected $htcontent = "Order Deny,Allow\nDeny From All\n\n<FilesMatch \"\.(gif|jpe?g|png|GIF|JPE?G|PNG)$\">\t\nAllow From All\n</FilesMatch>\n";
		
		protected $drafts;
		
		protected function read_permissions(){
			parent::read_permissions();
			if(!file_exists($this->realpath('.htaccess'))){
				$this->mkfil('.htaccess', array('contents'=>$this->htcontent));							
			}				
		}
	
		function has_draft($file){
			if(!$this->pkg->is_fmgr_file($file)) return false;
			return is_dir($this->realpath($file->id));
		}
	
		private function draft_obj($file){
			if(!$this->has_draft($file)) return false;
			if(!isset($this->drafts[$file->id])){
				$draft = $this->child($file->id);
				if(!is_array($draft)) return $this->drafts[$file->id] = $draft;
				foreach($draft as $d) $d->destroy();
				return false;
			}
			return $this->drafts[$file->id];
		}
	
		private function mkdraft($file){
			if(!$this->pkg->is_fmgr_file($file) || !$file->is_readable()) return false;
			if(!$draft = $this->draft_obj($file)) $draft = $this->mkdir($file->id, false, 1);
			return $draft;
		}
		
		function do_draft($file, $index=false){
			if(!$draft = $this->mkdraft($file)) return false;
			return $draft->redraft($index);
		}
		
		function undraft($file){
			if(!$draft = $this->draft_obj($file)) return true;
			if(!$draft->destroy()) return false;
			unset($this->drafts[$file->id]);
			return true;
		}
		
		function rewrite($file, $dfil){
			if(!$draft = $this->draft_obj($file)) return true;
			return $draft->rewrite($dfil);
		}
		
		function discard($file, $dfil){
			if(!$draft = $this->draft_obj($file)) return true;
			return $draft->discard($dfil);
		}
		
		function latest_of($file){
			if(!$draft = $this->draft_obj($file)) return $file;
			return $draft->latest();
		}
		
		function index_of($file){
			if(!$draft = $this->draft_obj($file)) return new obj;
			return $draft->meta();
		}
	
	}

?>